package com.eyaomall.wxmanage.common.advice;

import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eyaomall.wxmanage.common.helper.MemberHelper;
import com.eyaomall.wxmanage.model.po.OperLog;
import com.eyaomall.wxmanage.service.OperLogService;
import com.lycheeframework.core.cmp.api.http.GsnData;


/**
 * 登录 日志跟踪
 * 
 * @author yuhuafeng
 *
 */
@Component
@Aspect
public class LoginOperLogAdvice {

	@Autowired
	private OperLogService operLogService;

	@Pointcut("execution(public * com.eyaomall.wxmanage.controller.LoginController.login(..))")
	public void pointCut() {
		// ...
	}

	@Before(value = "pointCut()")
	public void before(JoinPoint joinPoint) {

	}

	/**
	 * 使用环绕通知来判断登录成功结果，并判断是否需要保存日志
	 * @param pjp
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Around(value = "pointCut()")
	public GsnData around(ProceedingJoinPoint pjp) {

		GsnData result = null;
		try {
			result = (GsnData) pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		Map<String, String> map=(Map<String, String>) result.getData();
		//判断是否登录成功
		if ("success".equalsIgnoreCase(map.get("flag").toString())) {
			Object[] s = null;
			String amount = "";
			s = pjp.getArgs();
			//获取amount，由于password也是string，所以获取到第一个string之后借宿
			for (Object obj : s) {
				if (obj instanceof String) {
					amount = (String) obj;
					break;
				}
			}
			OperLog log = new OperLog();
			log.setOwnerId(MemberHelper.getId());
			log.setContent("登录成功用户名为：" + amount);
			log.setType("4");
			operLogService.create(log);
		}
		return result;
	}

}
